package com.woniuxy.yogaapp.handler;

import com.woniuxy.yogaapp.pojo.Attention;
import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.Consultation;
import com.woniuxy.yogaapp.pojo.Friends;
import com.woniuxy.yogaapp.service.RelationService;
import com.woniuxy.yogaapp.util.ShiroSession;
import com.woniuxy.yogaapp.util.TimeUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 处理有好友，关注等方面的请求
 */
@RestController
@RequestMapping("/relation")
@Scope("prototype")
public class RelationHandler {

	@Resource
	private RelationService relationService;

	/**
	 * 添加关注
	 * 
	 * @param other
	 * @return
	 */
	@RequestMapping("/addAttention")
	public String addAttention(Integer other) {
		String result;
		Integer me = ShiroSession.getUid();
		// 拒绝非法访问
		if (me == null || me == 0 || other == null || other == 0) {
			result = "";
			return result;
		}

		// 生成关注对象
		Attention attention = new Attention(null, me, other, null, null, null);
		// 生成关注通知对象
		Chat chat = new Chat(null, me, other, null, "未读", TimeUtil.getNowDate(), 3);

		// 添加关注
		result = relationService.addAttention(attention, chat);

		return result;
	}

	/**
	 * 取消关注
	 * 
	 * @param me
	 * @param other
	 * @return
	 */
	@RequestMapping("/cancelAttention")
	public String cancelAttention(Integer me, Integer other) {
		String result;
		// 拒绝非法访问
		if (me == null || me == 0 || other == null || other == 0) {
			result = "";
			return result;
		}

		// 生成取消关注对象
		Attention attention = new Attention(null, me, other, null, null, null);
		// 生成取消关注通知对象
		Chat chat = new Chat(null, me, other, null, "未读", TimeUtil.getNowDate(), 4);

		// 取消关注
		result = relationService.cancelAttention(attention, chat);

		return result;
	}

	/**
	 * 查找用户的所有好友的名字和头像
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping("/findFriend")
	public List<Friends> findFriends(Integer uid) {
		// 非法访问
		if (uid == null || uid == 0) {
			return null;
		}
		return relationService.findFriends(uid);
	}

	/**
	 * 查找用户的所有关注人的名字和头像
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping("/findAttention")
	public List<Attention> findAttentions(Integer uid) {
		// 非法访问
		if (uid == null || uid == 0) {
			return null;
		}
		return relationService.findAttentions(uid);
	}

	/**
	 * 添加咨询关系
	 * 
	 * @param other
	 * @return
	 */
	@RequestMapping("/addConsultation")
	public String addConsultation(Integer other) {

		// 获取uid并合法性判断
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		Integer me = (Integer) session.getAttribute("uid");
		System.out.println(me + "  " + other);
		if (other == null || other == 0 || me == null || me == 0) {
			return null;
		}
		return relationService.addConsultation(me, other);
	}



    /**
     * 根据uid查询咨询对象
     * @param uid
     * @return
     */
    @RequestMapping("/findConsultationByUid")
    public List<Consultation> findConsultationByUid(Integer uid) {
        if (uid == null || uid == 0) {
            return null;
        }
        return relationService.findConsultationByUid(uid);
    }

    /**
     *
     * @param me
     * @param other
     * @return
     */
    @RequestMapping("/deleteConsultation")
    public String deleteConsultation(Integer me, Integer other) {
        if (me == null || me == 0 || other == null || other == 0) {
            return null;
        }
        return relationService.deleteConsultation( me, other);
    }

}
